package com.tsd.core.utils;

import cn.hutool.core.io.IoUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * pdf工具类
 *
 * @author Hillpool
 */
public class PdfUtils {
    // 单页拆分PDF文件 按顺序
    public static void spitPdfInPages(String path) throws Exception {
        PdfReader reader = new PdfReader(path);
        // 获取目标PDF共多少页
        int n = reader.getNumberOfPages();
        // 保存文件路径数组
        ArrayList<String> savePaths = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            String savepath = path + '-' + i + ".pdf";
            savePaths.add(savepath);
        }
        // 循环目标每页
        for (int i = 0; i < n; i++) {
            FileOutputStream outputStream = null;
            try {
                Document document = new Document(reader.getPageSize(1));
                outputStream = new FileOutputStream(savePaths.get(i));
                PdfCopy copy = new PdfCopy(document, outputStream);
                document.open();
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, i + 1);
                copy.addPage(page);
                copy.close();
                document.close();
            } finally {
                IoUtil.close(outputStream);
            }
        }
    }

    /**
     * 合并PDF文件
     *
     * @param files
     * @param savepath
     */
    public static void mergePdfFiles(String[] files, String savepath) {
        try {
            Document document = new Document(new PdfReader(files[0]).getPageSize(1));
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
            document.open();
            for (int i = 0; i < files.length; i++) {
                PdfReader reader = new PdfReader(files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片转PDF文件
     *
     * @param imgFiles
     * @param savepath
     */
    public static void images2Pdf(String[] imgFiles, String savepath) {
        try {
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(savepath));
            document.open();
            for (int i = 0; i < imgFiles.length; i++) {
                Image image1 = Image.getInstance(imgFiles[i]);
                image1.scaleToFit(PageSize.A4);
                document.add(image1);
            }
            document.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
    }
}
